from machine import I2C,SPI, Pin #导入通信以及引脚相关的库
import st7789 #导入屏幕驱动库
import utime
import math
i2c=  I2C(1, scl=Pin(1), sda=Pin(0), freq=400000) #构造IIC对象

st=21 #成像时行列像素
spi = SPI(0, baudrate=40000000, polarity=1, phase=0, bits=8, endia=0, sck=Pin(6), mosi=Pin(8)) #构造spi对象
display = st7789.ST7789(spi, 240, 240, reset=Pin(11,func=Pin.GPIO, dir=Pin.OUT), dc=Pin(7,func=Pin.GPIO, dir=Pin.OUT)) #构造屏幕控制对象
display.init() #屏幕初始化

backgroud_clr = st7789.color565(255, 255, 255) #设置背景颜色为白色
obj_clr = st7789.color565(0, 0, 0) #温度显示用颜色
display.fill(backgroud_clr) #屏幕背景颜色填充

obj_len = 12 #每个像素的屏幕显示尺寸
user_rows = st #每列16像素
user_columns = st #每行16像素
MAXTEMP=27 #最高温度
MINTEMP=25 #最低温度
start_x = 0
start_y = 0

camColors=[0x480F,
0x400F,0x400F,0x400F,0x4010,0x3810,0x3810,0x3810,0x3810,0x3010,0x3010,
0x3010,0x2810,0x2810,0x2810,0x2810,0x2010,0x2010,0x2010,0x1810,0x1810,
0x1811,0x1811,0x1011,0x1011,0x1011,0x0811,0x0811,0x0811,0x0011,0x0011,
0x0011,0x0011,0x0011,0x0031,0x0031,0x0051,0x0072,0x0072,0x0092,0x00B2,
0x00B2,0x00D2,0x00F2,0x00F2,0x0112,0x0132,0x0152,0x0152,0x0172,0x0192,
0x0192,0x01B2,0x01D2,0x01F3,0x01F3,0x0213,0x0233,0x0253,0x0253,0x0273,
0x0293,0x02B3,0x02D3,0x02D3,0x02F3,0x0313,0x0333,0x0333,0x0353,0x0373,
0x0394,0x03B4,0x03D4,0x03D4,0x03F4,0x0414,0x0434,0x0454,0x0474,0x0474,
0x0494,0x04B4,0x04D4,0x04F4,0x0514,0x0534,0x0534,0x0554,0x0554,0x0574,
0x0574,0x0573,0x0573,0x0573,0x0572,0x0572,0x0572,0x0571,0x0591,0x0591,
0x0590,0x0590,0x058F,0x058F,0x058F,0x058E,0x05AE,0x05AE,0x05AD,0x05AD,
0x05AD,0x05AC,0x05AC,0x05AB,0x05CB,0x05CB,0x05CA,0x05CA,0x05CA,0x05C9,
0x05C9,0x05C8,0x05E8,0x05E8,0x05E7,0x05E7,0x05E6,0x05E6,0x05E6,0x05E5,
0x05E5,0x0604,0x0604,0x0604,0x0603,0x0603,0x0602,0x0602,0x0601,0x0621,
0x0621,0x0620,0x0620,0x0620,0x0620,0x0E20,0x0E20,0x0E40,0x1640,0x1640,
0x1E40,0x1E40,0x2640,0x2640,0x2E40,0x2E60,0x3660,0x3660,0x3E60,0x3E60,
0x3E60,0x4660,0x4660,0x4E60,0x4E80,0x5680,0x5680,0x5E80,0x5E80,0x6680,
0x6680,0x6E80,0x6EA0,0x76A0,0x76A0,0x7EA0,0x7EA0,0x86A0,0x86A0,0x8EA0,
0x8EC0,0x96C0,0x96C0,0x9EC0,0x9EC0,0xA6C0,0xAEC0,0xAEC0,0xB6E0,0xB6E0,
0xBEE0,0xBEE0,0xC6E0,0xC6E0,0xCEE0,0xCEE0,0xD6E0,0xD700,0xDF00,0xDEE0,
0xDEC0,0xDEA0,0xDE80,0xDE80,0xE660,0xE640,0xE620,0xE600,0xE5E0,0xE5C0,
0xE5A0,0xE580,0xE560,0xE540,0xE520,0xE500,0xE4E0,0xE4C0,0xE4A0,0xE480,
0xE460,0xEC40,0xEC20,0xEC00,0xEBE0,0xEBC0,0xEBA0,0xEB80,0xEB60,0xEB40,
0xEB20,0xEB00,0xEAE0,0xEAC0,0xEAA0,0xEA80,0xEA60,0xEA40,0xF220,0xF200,
0xF1E0,0xF1C0,0xF1A0,0xF180,0xF160,0xF140,0xF100,0xF0E0,0xF0C0,0xF0A0,
0xF080,0xF060,0xF040,0xF020,0xF800]

dest=[0]*(st*st)
IRdata=[0]*(8*8)
addr=105
bin_data = [0]*128
src_w=8
src_h=8
des_h=st
des_w=st
while True:
    bin_data=list(i2c.readfrom_mem(addr,0x80,128))
    for i in range (64): 
        IRdata[i]=int((bin_data[i*2+1]*256+bin_data[i*2])*0.25*1000)

    srcX=0
    srcY=0
    x=start_x
    y=start_y

    for dstY in range(des_h):
        for dstX in range(des_w):
            srcX=(dstX+0.5)* (src_w/des_w)-0.5
            srcY=(dstY+0.5)*(src_h/des_h)-0.5
            src_x_0 = int(math.floor(srcX))
            src_y_0 = int(math.floor(srcY))
            src_x_1 = min(src_x_0 + 1, src_w - 1)
            src_y_1 = min(src_y_0 + 1, src_h - 1)
            value0 = (src_x_1 - srcX) * IRdata[src_y_0*src_w+src_x_0] + (srcX - src_x_0) * IRdata[src_y_0*src_w+src_x_1]
            value1 = (src_x_1 - srcX) * IRdata[src_y_1*src_w+src_x_0] + (srcX - src_x_0) * IRdata[src_y_1*src_w+src_x_1]
            if(dstX!=(des_w-1)and dstY!=(des_h-1)):
                dest[dstY*des_w+dstX] = int((src_y_1 - srcY) * value0 + (srcY - src_y_0) * value1)
                obj_clr=camColors[int((max(min(dest[dstY*des_w+dstX],MAXTEMP*1000),MINTEMP*1000)-MINTEMP*1000)*(255/(MAXTEMP*1000-MINTEMP*1000)))]

                display.fill_rect(x,y,obj_len,obj_len, obj_clr)
            x += obj_len
        x = start_x
        y += obj_len




